import 'package:get/get.dart';
import 'package:enterprise_v8/app/core/utils/interface.dart';
import 'package:enterprise_v8/app/core/utils/network.dart';
import 'package:enterprise_v8/app/core/utils/user_manager.dart';
import 'package:enterprise_v8/app/data/api/auth_apis.dart';
import 'package:enterprise_v8/router/app_pages.dart';

class SplashControl extends GetxController {

  /// 失败提示
  final error = ''.obs;

  /// 初始化阿里云接口
  _init() async {
    final res = await AuthApis.getUrl().execute();
    // 成功后
    res.onSuccess((value) async {
      Interface.mainBaseUrl = value.data!.gsPaMainApiUrl!;

      /// 文件上传地址
      Interface.fileUrl = value.data!.gsFileUrl!;

      Interface.mapUrl = value.data!.threeDimensionalMap ?? '';

      Interface.workUrl = value.data!.jobTicketAddress ?? '';

      if (value.data!.pullAddress != null) {
        Interface.pullUrl = '${value.data!.pullAddress!}/ai_live/';
      }

      if (value.data!.pushAddress != null) {
        Interface.rtmpUrl = '${value.data!.pushAddress!}/live';
      }

      if (!UserManager.isLoggedIn) {
        Get.offAndToNamed(Routes.authLogin);
      } else {
        Get.offAndToNamed(Routes.home);
      }
      

    });

    // 失败后
    res.onError((result) {
      error.value = '获取接口地址失败';
    });

  }

  @override
  void onInit() {
    super.onInit();
    NetworkUtil.init();
    _init();
  }

}